<template>
  <div class="login-wrapper" :style="{ 'background-image': 'url(' + src + ')' }">
    <div class="login-left">
     
    </div>
    <div class="login-right">
      <div class="container">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
      </div>
      <el-form
        style="width:400px"
        ref="loginForm"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="80px"
      >
        <el-form-item label="" prop="username">
          <el-input
            v-model.trim="loginForm.username" placeholder="请输入用户名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password" >
          <el-input
            type="password" 
            v-model.trim="loginForm.password" placeholder="请输入密码"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="submitLoginForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as vueJsonp from "vue-jsonp";
export default {
  data() {
    return {
      src: "",
      loginForm: {
        username: "",
        password: "",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.setBackgroundImage();
  },
  methods: {
    async setBackgroundImage() {
      let url = `https://bing.ioliu.cn/v1/`;
      vueJsonp
        .jsonp(url, {
          params: { d: 0, w: window.innerWidth, h: window.innerHeight },
        })
        .then((res) => {
          console.log(1, res);
          this.src = res.data.url;
        });
    },
    submitLoginForm() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // 宝宝的小贴士：表单验证通过后，可以在这里调用登录API接口进行登录操作
          this.loginForm.name = this.loginForm.username
          this.loginForm.pwd = this.loginForm.password
          var res = await this.http.post('/api/login',this.loginForm )
          if(!res.code){
             console.log(res)
             localStorage.setItem('token',res.token)
             localStorage.setItem('name',this.loginForm.username)
             this.$message.success("登录成功")
             this.$router.push('/home');
          }else{
            this.$message.error("登录失败")
          }
          // 如果登录成功，可以跳转到其他页面；如果登录失败，可以给出提示信息
        }
      });
    },
  },
};
</script>
<style>
  body{
    margin: 0;
    padding: 0;
  }
   
.login-wrapper {
  display: flex;
  height: 100vh;
}
.login-left {
  flex: 2;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
}
.login-left img {
  width: 120px;
  margin-bottom: 20px;
}
.login-left h2 {
  font-size: 24px;
}
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-form {
  width: 360px;
}
.form-title {
  text-align: center;
  font-size: 20px;
  margin-bottom: 20px;
}
</style>

